package com.happe.foodeasy.winter;

import com.happe.foodeasy.constraint.annotation.EntityCustom;
import com.happe.foodeasy.helper.AnnotationHelper;
import com.happe.foodeasy.winter.conf.parser.ConfInformationParseResult;
import com.happe.foodeasy.winter.conf.parser.WinterConfInformationParser;
import com.happe.foodeasy.winter.helper.WinterSqlHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * This class is used to generate tables in database.
 * Created by wubin on 2016-05-18.
 */
public class TableOperator {
    private static final String DO_NOTHING_LABEL = "nothing";
    private static final String CREATE_LABEL = "create";
    @SuppressWarnings("all")
    private static final String DELETE_LABEL = "delete";
    private WinterConfInformationParser parse;

    private WinterSqlHelper winterSqlHelper;

    /*
    * this property is used to mark the operator when spring launches,
    * 1)create: means delete the previous tables in database, and create them again.
    * 2)delete: means delete the previous tables, and do nothing then.
    * 3)nothing: do nothing.
    * */
    private String sqlOperator = "create";

    public void setParse(WinterConfInformationParser parse) {
        this.parse = parse;
    }

    public void setSqlOperator(String sqlOperator) {
        this.sqlOperator = sqlOperator;
    }

    public void setWinterSqlHelper(WinterSqlHelper winterSqlHelper){
        this.winterSqlHelper = winterSqlHelper;
    }

    /**
     * This method is used to operate all tables when spring runs.
     */
    public void createAllTablesForEntities() {
        if (sqlOperator.equals(DO_NOTHING_LABEL)) {
            return;
        }
        ConfInformationParseResult result = parse.startParse();
        Class[] hasEntityCustomAnnotationClasses = retrieveTargetClassesInPackageNames(result.retrieveTargetPackageNames());

        deleteAllTables(hasEntityCustomAnnotationClasses);

        if (sqlOperator.equals(CREATE_LABEL)) {
            for (Class oneClass : hasEntityCustomAnnotationClasses) {
                createTable(oneClass);
            }
        }
    }

    private void deleteAllTables(Class[] hasEntityCustomAnnotationClasses) {

    }

    private void createTable(Class<?> type) {
        String createSql = winterSqlHelper.generateCreateTableSql(type);
        System.out.println(createSql + "\n");
    }

    private Class[] retrieveTargetClassesInPackageNames(String[] packageNames) {
        List<Class[]> result = new ArrayList<>();
        for (String packageName : packageNames) {
            try {
                result.add(AnnotationHelper.parseMarkAnnotationAndPackage(EntityCustom.class, packageName));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        List<Class> allClass = new ArrayList<>();
        for (Class[] oneClassResult : result) {
            Collections.addAll(allClass, oneClassResult);
        }

        return allClass.toArray(new Class[allClass.size()]);
    }
}
